<template>
	<view class="page_box" style="padding-bottom: 100rpx;">
		<view class="head_box ">
			<yy-custom bgColor="bg-gradual-blue" v-if="0">
				<block slot="backText">返回</block>
				<block slot="content">导航栏</block>
			</yy-custom>
		</view>

		<view class="content_box">
			<!-- <view class="prompt padding-lr padding-tb-sm" style="background: #FFF7EB;">
				<text class="cuIcon-notice margin-right text-yellow"></text>
				<text class="text-yellow f26  ">您最低提货总数量需达到10盒</text>
			</view> -->
			<view class="theWay padding-lr padding-tb flex justify-between align-center bg-white">
				<text class="text-gray f28">选择提货方式</text>
				<view class="flex ">
					<u-radio-group v-model="value">
						<u-radio @change="radioChange" active-color="#FBB03B" v-for="(item, index) in list" :key="index"
							:name="item.name" :disabled="item.disabled">
							{{ item.name }}
						</u-radio>
					</u-radio-group>
				</view>
			</view>
			<view class="addr  padding-lr padding-tb  flex justify-between bg-white margin-top" v-if="selfMention == 1">
				<view class="flex align-center">
					<image src="../../static/dz1.png" mode="aspectFill" style="width: 28rpx; height: 32rpx;"></image>
					<!-- <input type="text" v-model="textInfo" disabled=" " placeholder="请选择收货信息" class="margin-left-sm"
						placeholder-class="ddd" /> -->
				</view>
				<text class="">{{textInfo}}</text>
			</view>

			<view class="addr  padding-lr padding-tb  flex justify-between bg-white margin-top align-center"
				v-if="selfMention == 2" @click="jump('/pages/personalCtr/addrManagement')">
				<view class="flex align-center" style="width: 100%;">
					<image src="../../static/dz1.png" mode="aspectFill" style="width: 28rpx; height: 32rpx;"></image>
					<view class="" style="margin-left: 20rpx;">
						<view>{{userInfoText ? userInfoText: ''}}</view>
						<view>{{zitiInfo ? zitiInfo: '请选择收货地址'}}</view>
						<view>{{detailText ? detailText : ''}}</view>
					</view>
					<!-- <textarea  type="text" v-model="zitiInfo" disabled=" " placeholder="请选择收货信息"
						style="width: 100%; height: 100rpx;"
						class="margin-left-sm1"
						placeholder-class="ddd" /> -->
				</view>
				<text class="cuIcon-right text-gray"></text>
			</view>

			<view class="addr  padding-lr padding-tb  flex justify-between bg-white margin-top" v-if="selfMention == 1">
				<view class="flex align-center">
					<image src="../../static/dhg.png" mode="aspectFill" style="width: 22rpx; height: 33rpx;"></image>

					<text class="margin-left-sm f30 text-black">总部电话：</text>
				</view>
				<view class=" flex text-gray f30 align-center" @tap="phoneInfo">
					18965230123
					<text class="cuIcon-copy margin-left text-gray"></text>
				</view>
			</view>

			<view class="addr  padding-lr padding-tb  flex justify-between bg-white margin-top" v-if="selfMention == 1">
				<view class="flex align-center">
					<image src="../../static/dhg.png" mode="aspectFill" style="width: 22rpx; height: 33rpx;"></image>

					<text class="margin-left-sm f30 text-black">个人电话：</text>
				</view>
				<view class=" flex text-gray f30 align-center">
					<!-- {{phoneInfo}} -->
					<input type="text" v-model="phoneInfo1" placeholder="请输入手机号" class="margin-left-sm"
						placeholder-class="ddd" />
					<!-- <text class="cuIcon-copy margin-left text-gray"></text> -->
				</view>
			</view>

			<view @click="timeShow = true" class="addr  padding-lr padding-tb  flex justify-between bg-white margin-top"
				v-if="selfMention == 1">
				<view class="flex align-center">
					<image src="../../static/dd2.png" mode="aspectFill" style="width: 28rpx; height: 28rpx;"></image>

					<input type="text" v-model="time" disabled=" " placeholder="请选择自提时间" class="margin-left-sm"
						placeholder-class="ddd" />
				</view>
				<view class=""><text class=" f30 cuIcon-right"></text></view>
			</view>

			<view class="addr  padding-lr padding-tb  flex justify-between bg-white margin-top" v-if="selfMention == 2">
				<view class="flex align-center">
					<image src="../../static/kff.png" mode="aspectFill" style="width: 28rpx; height: 23rpx;"></image>

					<text class="margin-left-sm f30 text-black">消耗积分</text>
				</view>
				<view class="">
					<text class="text-gray f30">{{jifen}}</text>
					<text class="cuIcon-question text-gray margin-left"></text>
				</view>
			</view>

			<view class="padding-lr-sm flex justify-between margin-top bg-white">
				<view class="info  flex flex-direction " v-for="(item, index) in repertory" :key="index">
					<view class="info-inside flex align-center justify-center ">
						<image
							src="http://ershoucwxxcx.oss-cn-shenzhen.aliyuncs.com/images/2021/05/10/16205883289157755.png"
							mode="aspectFill" style="width: 279rpx; height: 235rpx;" class="bg-gray"></image>
						<view class="info-inside-transparent flex justify-center align-center"
							style="white-space: nowrap;">库存：{{item.repertoryNum}}
						</view>
					</view>

					<text class="text-black f30 margin-top-sm">{{item.name}}</text>
					<!-- <text class="text-gray f26 margin-top-sm">净含量：{{item.size}}</text> -->
					<view class="flex margin-top-sm">
						<text class="f28 text-black " style="white-space: nowrap;">数量：</text>
						<input type="number" @input="inputInfo(item.id)" v-model="item.num" value="numInfoInput"
							class="solid-bottom margin-left text-center" placeholder="请输入" placeholder-class="ddd" />

						<!-- <input type="number" v-model="item.num" value="numInfoInput"
								class="solid-bottom margin-left text-center" placeholder="请输入" placeholder-class="ddd" /> -->
					</view>
				</view>
			</view>
			<view class="margin-top-xl padding-lr padding-tb-xs fixed bg-white  flex align-center justify-between">
				<view class="flex align-center">
					<text class="f26 text-black">合计(箱)：</text>
					<text class="margin-left text-red text-bold">{{numInfo ? numInfo: 0 }}</text>
				</view>
				<button class="cu-btn btn flex align-center justify-center" @tap="addApply">提交申请</button>
			</view>
		</view>
		<view class="foot_box">
			<u-popup v-model="show" mode="bottom" border-radius="50">
				<view class="padding red popup">
					<view class="flex justify-center margin-tb"><text
							class="f34 text-bold text-black text-center">选择收货地址</text></view>
					<scroll-view scroll-y="true" style="height: 55vh;">

						<!-- <view class="info padding flex bg-white radius margin-bottom"
							>
							<image src="../../static/xz.png" mode="aspectFill" style="width: 26rpx; height: 26rpx;"></image>
							<view class="flex flex-direction margin-left ">
								<view class="flex">
									<text class="f26 text-black">提交申请</text>
									<text class="f26 text-black margin-left">18989651235</text>
									<view class="def flex justify-center align-center margin-left">默认地址</view>
								</view>
								<text class="f24 text-gray margin-top-sm ">成都市金牛区草市街天盛壹中心二栋二单元18.....</text>
							</view>
						</view> -->

						<view class="info padding flex bg-white radius margin-bottom" v-for="(item, index) in dataList"
							:key="index" @tap="diqu(item, index)">
							<!-- <image src="../../static/xz.png" mode="aspectFill" style="width: 26rpx; height: 26rpx;"></image> -->
							<view class="flex flex-direction margin-left ">
								<view class="flex">
									<text class="f26 text-black">{{item.name}}</text>
									<text class="f26 text-black margin-left">{{item.phone}}</text>
									<!-- <view class="def flex justify-center align-center margin-left">默认地址</view> -->
								</view>
								<text class="f24 text-gray margin-top-sm ">{{item.detail}}</text>
								<text class="f24 text-gray margin-top-sm ">{{item.detail}}</text>
							</view>
						</view>
					</scroll-view>
					
					<button class="btn1 cu-btn" @tap="jump('/pages/personalCtr/addShipAddr')">新增收货地址</button>
				</view>
			</u-popup>
		</view>
		<u-picker v-model="timeShow" mode="time" @confirm="confirm" :params="params" @cancel="cancel"></u-picker>
		<!-- 登录提示 -->
		<yy-login v-if="false"></yy-login>
		<!-- 加载提示 -->
		<yy-loading text="加载中.." mask="true" click="true" ref="loading"></yy-loading>
		<!-- 信息提示-->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				list: [{
						name: '自提',
						disabled: false
					},
					{
						name: '快递',
						disabled: false
					}
				],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				value: '自提',
				show: false,
				timeShow: false,
				selfMention: 1, // 1==自提 2==快递
				time: '',
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: false
				},
				dataList: [],
				isactive: 0,
				listFrom: [{
					name: 'apple',
					disabled: false
				}],
				value: '',
				textInfo: '',
				zitiInfo: '',
				detailText: '',
				userInfoText: '',
				phone: '',
				areaId: '',
				repertory: [],
				numInfo: 0,
				numInfoInput: '',
				jifen: 0,
				searchVal: {},
				phoneInfo1: '',
				orderList: [],
				orderId: '',
				provinceName: '',
				// inputInfo: '',
			};
		},
		onShow() {
			if (this.searchVal.id !== undefined) {
				this.areaId = this.searchVal.id
				this.phone = this.searchVal.phone
				this.provinceName = this.searchVal.province
				// this.detailText = this.searchVal.detail 
				this.userInfoText = this.searchVal.name + '-' + this.searchVal.phone
				this.zitiInfo = this.searchVal.province + '-' + this.searchVal.county + '-' + this.searchVal.city
				this.initNum()
				console.log('传递过来的Id地址', this.areaId)
				console.log('传递过来的地址', this.searchVal.province)
			}

			// this.$refs.loading.close()
			//关闭加载动画
		},
		onLoad(e) {
			this.invInq()
			this.getList()
			this.getListText()
			// this.$refs.loading.open();
		},
		methods: {
			jump(url) {
				uni.navigateTo({
					url
				});
			},
			// 选中某个单选框时，由radio时触发
			radioChange(e) {
				e == '自提' ? (this.selfMention = 1) : (this.selfMention = 2);
			},

			initNum() {

				// 西藏地区
				if (this.provinceName == '新疆维吾尔自治区' || this.provinceName == '西藏自治区') {
					const unm = this.numInfo
					this.jifen = +25 + ((unm - 1) * 12)

					// 省内
				} else if (this.provinceName == '四川省') {
					const unm = this.numInfo
					this.jifen = +10 + ((unm - 1) * 5)
					// 省外 
				} else {
					const unm = this.numInfo
					this.jifen = +16 + ((unm - 1) * 8)
				}
			},

			confirm(e) {
				this.time = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute;
			},
			cancel() {
				this.time = '';
			},

			async getList() {
				let res = await this.$u.get('/harvestaddress/list/' + uni.getStorageSync('data').id, {}, {});
				if (res.code == 0) {
					this.dataList = res.data
				}
			},

			async getListText() {
				let res = await this.$u.get('/harvestaddress/headquarters', {}, {});
				if (res.code == 0) {
					this.textInfo = res.data[0].province + res.data[0].county + res.data[0].detail
					this.areaId = res.data[0].id
				}
			},

			diqu(item, index) {
				this.show = false
				this.isactive = index;
				// this.textInfo = item.name + ' ' + item.detail
				this.phone = item.phone
				this.areaId = item.id
				console.log(item)
			},

			phoneInfo() {
				uni.makePhoneCall({
					phoneNumber: '18965230123' //仅为示例
				});
			},
			// 库存查询
			async invInq() {
				// uni.getStorageSync('data').id
				let res = await this.$u.get('/repertory/' + uni.getStorageSync('data').id, {}, {});
				this.repertory = res.data.repertory;
				this.repertory.forEach((i => {
					if (i.num == 0) {
						i.num = ''
					}
				}))


			},

			jump1(url, index) {
				uni.switchTab({
					url: '/pages/invManagement/invManagement'
				});

			},


			async addApply() {
				
				

				// 自提
				if (this.selfMention == 1) {
					// var data = Boolean(this.repertory[0].num)
					// console.log("data: ", data);
					// var data1 = Boolean(this.repertory[1].num)
					// console.log("data1: ", data1);

					var data = this.repertory[0].num
					var repertoryNum = this.repertory[0].repertoryNum
					var repertoryNum1 = this.repertory[1].repertoryNum
					var data1 = this.repertory[1].num

					var heji = repertoryNum1 + repertoryNum


					if (data && data1) {
						if (heji < this.numInfo) return this.$refs.uToast.show({
							title: '库存不足!',
							type: 'error'
						});
						this.applay3()
						console.log("全部: 自提", );
					} else if (data && !data1) {
						if (data > repertoryNum) return this.$refs.uToast.show({
							title: '库存不足!',
							type: 'error'
						});
						this.applay1()
						console.log("第一个:自提 ", )
					} else if (!data && data1) {
						if (data1 > repertoryNum1) return this.$refs.uToast.show({
							title: '库存不足!',
							type: 'error'
						});
						this.applay2()
						console.log("第二个:自提 ", )
					} else {
						console.log("-----------------: ", )
					}


					// 快递
				} else if (this.selfMention == 2) {
					// if (this.jifen == '') return this.$refs.uToast.show({ title: '', type: 'none' });
					// var data = Boolean(this.repertory[0].num)
					// console.log("data: ", data);
					// var data1 = Boolean(this.repertory[1].num)
					// console.log("data1: ", data1);

					var data = this.repertory[0].num
					var repertoryNum = this.repertory[0].repertoryNum
					var repertoryNum1 = this.repertory[1].repertoryNum
					var data1 = this.repertory[1].num

					var heji = repertoryNum1 + repertoryNum
					console.log("heji: ", heji);
					if (data && data1) {
						if (heji < this.numInfo) return this.$refs.uToast.show({
							title: '库存不足!',
							type: 'error'
						});
						this.applay4()
						console.log("全部: ", );
					} else if (data && !data1) {
						if (data > repertoryNum) return this.$refs.uToast.show({
							title: '库存不足!',
							type: 'error'
						});
						this.applay5()
						console.log("第一个:快递 ", )
					} else if (!data && data1) {
						if (data1 > repertoryNum1) return this.$refs.uToast.show({
							title: '库存不足!',
							type: 'error'
						});
						console.log("第二个:快递 ", )
						this.applay6()
					} else {

					}

				}
			},


			async applay1() {
				const obj = {
					id: uni.getStorageSync('data').id,
					tel: Number(this.phoneInfo1),
					addressId: this.areaId,
					type: 1,
					pickDt: this.time,
					order: [{
						commodityId: this.repertory[0].id,
						num: Number(this.repertory[0].num)
					}]
				}
				let res = await this.$u.post('/order/pickApply', obj, {});
				if (res.code == 0) {
					this.$refs.uToast.show({
						title: '申请成功!',
						type: 'none'
					});

					var that = this

					setTimeout(function() {
						that.jump1()
					}, 2000)
				} else {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'error'
					});
				}


			},

			async applay2() {
				const obj = {
					id: uni.getStorageSync('data').id,
					tel: Number(this.phoneInfo1),
					addressId: this.areaId,
					type: 1,
					pickDt: this.time,
					order: [{
						commodityId: this.repertory[1].id,
						num: Number(this.repertory[1].num)
					}]
				}
				let res = await this.$u.post('/order/pickApply', obj, {});
				if (res.code == 0) {
					this.$refs.uToast.show({
						title: '申请成功!',
						type: 'none'
					});

					var that = this

					setTimeout(function() {
						that.jump1()
					}, 2000)
				} else {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'error'
					});
				}
			},

			async applay3() {
				const obj = {
					id: uni.getStorageSync('data').id,
					tel: Number(this.phoneInfo1),
					addressId: this.areaId,
					type: 1,
					pickDt: this.time,
					order: [{
						commodityId: this.repertory[0].id,
						num: Number(this.repertory[0].num)
					}, {
						commodityId: this.repertory[1].id,
						num: Number(this.repertory[1].num)
					}]
				}
				let res = await this.$u.post('/order/pickApply', obj, {});
				if (res.code == 0) {
					this.$refs.uToast.show({
						title: '申请成功!',
						type: 'none'
					});

					var that = this

					setTimeout(function() {
						that.jump1()
					}, 2000)
				} else {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'error'
					});
				}
			},



			async applay4() {
				const obj = {
					id: uni.getStorageSync('data').id,
					score: this.jifen,
					addressId: this.areaId,
					type: 0,
					order: [{
						commodityId: this.repertory[0].id,
						num: Number(this.repertory[0].num)
					}, {
						commodityId: this.repertory[1].id,
						num: Number(this.repertory[1].num)
					}]
				}
				let res = await this.$u.post('/order/pickApply', obj, {});
				if (res.code == 0) {
					this.$refs.uToast.show({
						title: '申请成功!',
						type: 'none'
					});

					var that = this

					setTimeout(function() {
						that.jump1()
					}, 2000)
				} else {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'error'
					});
				}

			},




			async applay5() {
				const obj = {
					id: uni.getStorageSync('data').id,
					// tel: Number(this.phoneInfo1),
					score: this.jifen,
					addressId: this.areaId,
					type: 0,
					order: [{
						commodityId: this.repertory[0].id,
						num: Number(this.repertory[0].num)
					}]
				}
				let res = await this.$u.post('/order/pickApply', obj, {});
				if (res.code == 0) {
					this.$refs.uToast.show({
						title: '申请成功!',
						type: 'none'
					});

					var that = this

					setTimeout(function() {
						that.jump1()
					}, 2000)
				} else {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'error'
					});
				}
			},


			async applay6() {
				const obj = {
					id: uni.getStorageSync('data').id,
					// tel: Number(this.phoneInfo1),
					addressId: this.areaId,
					score: this.jifen,
					type: 0,
					order: [{
						commodityId: this.repertory[1].id,
						num: Number(this.repertory[1].num)
					}]
				}
				let res = await this.$u.post('/order/pickApply', obj, {});
				if (res.code == 0) {

					this.$refs.uToast.show({
						title: '申请成功!',
						type: 'none'
					});

					var that = this

					setTimeout(function() {
						that.jump1()
					}, 2000)

				} else {
					this.$refs.uToast.show({
						title: res.msg,
						type: 'error'
					});
				}
			},



			inputInfo(e) {
				this.numInfo = Number(this.repertory[0].num) + Number(this.repertory[1].num)
				console.log('eeeeeeeeeeeeeeeeeeee', this.numInfo)
				this.initNum()
				this.orderId = e
			},

		}
	};
</script>

<style lang="scss" scoped>
	.page_box {
		background-color: #f8f8f8;
		margin-top: -5rpx;

		.content_box {
			.addr {
				/deep/input {
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #717171;
				}

				.ddd {
					font-size: 20rpx !important;
					font-family: PingFang SC;
					font-weight: 500;
					color: #b8b8b8;
				}
			}

			.fixed {
				width: 100vw;
				position: fixed;
				left: 0;
				transform: translateX(-50);
				bottom: 0;

				.btn {
					width: 210rpx;
					height: 66rpx;
					background: #5b8180;
					border-radius: 20rpx;

					font-size: 28rpx;

					font-weight: 500;
					color: #ffffff;
				}
			}

			.bg {
				background-color: #A5673F;
			}

			.info {
				width: 340rpx;

				.info-inside {
					position: relative;
					width: 340rpx;
					height: 340rpx;
					background: #ffffff;
					border-radius: 20rpx;

					.info-inside-transparent {
						position: absolute;
						top: 50%;
						left: 50%;
						transform: translate(-50%, -50%);
						width: 180rpx;
						height: 50rpx;
						background: #000000;
						opacity: 0.4;

						font-family: PingFang SC;
						font-weight: bold;
						color: #ffffff;
					}
				}
			}
		}

		.foot_box {
			.popup {
				width: 750rpx;
				height: 987rpx;
				background: #f8f8f8;

				.red {
					background: #f7f7f7;
				}

				.def {
					width: 100rpx;
					height: 28rpx;
					background: #ffffff;
					border: 1rpx solid #fbb03b;

					font-size: 22rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #fbb03b;
				}

				.btn1 {
					position: fixed;
					bottom: 15rpx;
					left: 50%;
					transform: translateX(-50%);
					width: 686rpx;
					height: 90rpx;
					background: #92b9b7;
					border-radius: 45rpx;

					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #ffffff;
				}
			}
		}
	}
</style>
